# 识别图片中的文字
# 使用 EasyOCR 进行图片文字识别
import easyocr
import os
import pandas as pd

# 初始化 reader
reader = easyocr.Reader(['ch_sim','en'])

def scanImageToText():
    # 读取图片目录
    base_folder = 'img/'
    results = []

    # 遍历目录下的所有图片
    for root, dirs, files in os.walk(base_folder):
        for file in files:
            if file.endswith(('.jpg', '.png', '.jpeg')):
                image_path = os.path.join(root, file)
                # 识别图片文字
                result = reader.readtext(image_path)
                text = ' '.join([item[1] for item in result])
                # 保存结果
                results.append({
                    'image_path': image_path,
                    'text': text
                })

    # 保存到CSV
    df = pd.DataFrame(results)
    df.to_csv('ocr_results.csv', index=False, encoding='utf-8')
    print('识别完成,结果已保存到 ocr_results.csv')

if __name__ == '__main__':
    scanImageToText()